import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import twitter4j.Paging;
import twitter4j.ProfileImage;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

import java.util.regex.Matcher;

public class TwitterUserModel {

	public final String twitterUserName;
	public final Image profileImage;
	public String twitterUserStatuses;
	public Date originalTweetTime;
	public final String link;

	public TwitterUserModel(String twitterUserName) {

		this.twitterUserName = twitterUserName;
		this.profileImage = getUserProfileImage();
		this.twitterUserStatuses = getUserTweets();
		this.originalTweetTime = getTweetTime();
		this.link = getLink();
	}

	public Image getUserProfileImage() {
		final Image img;
		try {
			Twitter twitter = new TwitterFactory().getInstance();
			ProfileImage.ImageSize imageSize = ProfileImage.BIGGER;
			ProfileImage image = twitter.getProfileImage(this.twitterUserName,
					imageSize);
			URL imageURL = new URL(image.getURL());
			img = ImageIO.read(imageURL);
			return img;
		} catch (TwitterException te) {
			System.out
					.println("Failed to get profile image (TwitterException) : "
							+ te.getMessage());
			te.printStackTrace();
		} catch (IOException e) {
			System.out.println("Failed to get profile image (IOEXception) ");
			e.printStackTrace();
		}
		return null;
	}

	public String getUserTweets() {
		final String tweet;
		try {
			Twitter unauthenticatedTwitter = new TwitterFactory().getInstance();
			Paging paging = new Paging(1, 10);
			List<Status> statuses = unauthenticatedTwitter.getUserTimeline(
					this.twitterUserName, paging);
			if(statuses.get(0).getText() != null){
				tweet = statuses.get(0).getText();
				return tweet;
			}
			else{
				JOptionPane.showConfirmDialog(null, "User has no tweets");
			}
		} catch (TwitterException te) {
			System.out
					.println("Failed to get TwitterUser's Status (TwitterException) : "
							+ te.getMessage());
			te.printStackTrace();
		}
		return null;
	}

	public Date getTweetTime() {
		Twitter twitter = TwitterFactory.getSingleton();
		Date tweetTime;
		try {
			tweetTime = twitter.getUserTimeline(this.twitterUserName).get(0)
					.getCreatedAt();
			return tweetTime;
		} catch (TwitterException e) {
			System.out.println("Unable to get tweet time");
			e.printStackTrace();
		}
		return null;
	}

	public boolean isTweetNew() {
		Date currentTweetTime = getTweetTime();
		System.out.println("The currentTweetTime is " + currentTweetTime
				+ " and the originalTweetTime is " + originalTweetTime);
		if (originalTweetTime.toString().equals(currentTweetTime.toString())) {
			System.out.println("Tweet time is the same for user "
					+ this.twitterUserName);
			return false;
		} else {
			System.out.println("New tweet");
			originalTweetTime = currentTweetTime;
			return true;
		}
	}

	private String getLink() {
		String regex = "http\\S+";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(this.twitterUserStatuses);
		while (m.find()) {
			String urlStr = m.group();
			return urlStr;
		}
		System.out.println("No links found");
		return null;

	}

	public void setTweetTime() {
		this.originalTweetTime = new Date();
	}

}
